/*
编写 : Hexh
时间 : 2012-5-25 10:31:04
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using HMIS.FrameWork;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using HMIS.Infrastructure.Facade;
using HMIS.Infrastructure.Core.Entities;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of InfraApproveStep
	/// </summary>
	public class InfraApproveStepController : BaseController
	{

		private readonly IInfraApproveStepFacade InfraApproveStepFacade;
		private readonly IHospUserFacade HospUserFacade;
		
		public InfraApproveStepController(IInfraApproveStepFacade InfraApproveStepFacade, IHospUserFacade HospUserFacade)
		{
			this.InfraApproveStepFacade = InfraApproveStepFacade;
			this.HospUserFacade = HospUserFacade;
		}
		
		[Description("访问InfraApproveStep管理页面")]
		[ViewPage]
		public ActionResult InfraApproveStep()
		{
			return View();
		}
		
		[ExtResult]
		[Description("添加修改InfraApproveStep信息")]
		public ActionResult SaveOrUpdate()
		{
			InfraApproveStep entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new InfraApproveStep
				{
					ApproveId = Request["ApproveId"], 
					ApproveStep = Request["ApproveStep"]==null ? 0 : int.Parse(Request["ApproveStep"]), 
					UserId = Request["UserId"], 
					Scale = Request["Scale"]==null ? 0 : int.Parse(Request["Scale"]),
					SmsStartTime = Request["SmsStartTime"] == null ? 0 : int.Parse(Request["SmsStartTime"]),
					SmsWarnTime = Request["SmsWarnTime"] == null ? 0 : int.Parse(Request["SmsWarnTime"]), 
					Description = Request["Description"]
				};
			}
			else
			{
				// get the entity.
				entity = InfraApproveStepFacade.Get(Request["Id"]);
				// set new value..
				//entity.ApproveId = Request["ApproveId"];  //这个不需要变了
				entity.ApproveStep = Request["ApproveStep"]==null ? 0 : int.Parse(Request["ApproveStep"]); 
				entity.UserId = Request["UserId"]; 
				entity.Scale = Request["Scale"]==null ? 0 : int.Parse(Request["Scale"]);
				entity.SmsStartTime = Request["SmsStartTime"] == null ? 0 : int.Parse(Request["SmsStartTime"]);
				entity.SmsWarnTime = Request["SmsWarnTime"] == null ? 0 : int.Parse(Request["SmsWarnTime"]); 
				entity.Description = Request["Description"]; 
			}			
			// ..设置外键相关信息..
			// 保存信息
			InfraApproveStepFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}
		
		[ExtResult]
		[Description("删除InfraApproveStep基本资料")]
		public ActionResult Delete()
		{
			InfraApproveStepFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
		
		[ExtResult]
		[Description("物理删除InfraApproveStep基本资料")]
		public ActionResult PhysicsDelete()
		{
			InfraApproveStepFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取InfraApproveStep清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = InfraApproveStepFacade.GetPlist(
				Request["q_query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			// 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<InfraApproveStep> { total = total, data = data },
				new string[] { "User" });
		}
		
		[Description("获取InfraApproveStep信息")]
		public ActionResult GetById()
		{
			var entity = InfraApproveStepFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<InfraApproveStep> { data = new InfraApproveStep[] { entity } }, new string[] { "" }
			   );
		}

		[ExtResult]
		[Description("判断InfraApproveStep字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = InfraApproveStepFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
